<?php


namespace App\Http\Controllers\Index;


use App\Http\Controllers\Controller;
use App\Model\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CategoryController extends Controller
{
    protected $param;
    public function __construct(Request $request)
    {
        if ($request->isMethod('GET')){
            $this->param = json_decode($request->input('params'),true);
        }elseif ($request->isMethod('POST')){
            $this->param = $request->input('params');
        }
    }

    public function get_category(){
        $cate = Category::with('product')
            ->where('is_home', '<>', 2)
            ->orderBy('sort','asc')
            ->get();
        return JsonResponse(200,true,'获取成功',getImageUrl($cate,'image'));
    }

    public function get_category_product(Request $request){
        $cate_id = $this->param['id'];
        $child = Category::with('product')
            ->where('id',$cate_id)
            ->first();
        $child->image = url($child->image);
        return JsonResponse(200,true,'获取成功',$child);
    }
}
